//@ sourceURL=userManager.js
$(function () {
    debugger
    if(localStorage.fxreload != "fxreload") {
        localStorage.fxreload = "fxreload";
        setTimeout(function () {
            location.reload()
        },800)
        return
    } else {
        localStorage.fxreload = "";
    }

    var tmplurl='../../scripts/plugins/jac.tmpl';
    _as.jAlertTmpl=tmplurl;
    var ad = appData(),curIndex,curentUsers={},tmplObj={},authObj={1:"大数据安全监管平台",2:"资源安全监管平台"};
    getTmpl()
    showUsers(1,1);

    $("#btnSearch").click(function () {
        showUsers(1,1);
    })

    $("#txt_keyword").on("keydown", function(event) {
        if (event.keyCode == 13) {
            showUsers(1, 1);
        }
    });

    $("#tbd_users").on("change",".user_state",function () {
        var user=curentUsers[$(this).parents("tr").attr("data-id")];
        user=$.extend({},user);
        user.status=this.checked? 1:0;
        //var check=this;
        ad.insert('NU_U_MODIFY',user,function () {
            curentUsers[user.id ].status= user.status;
            //this.checked=user.status;
        })
    })

    $("#tbd_users").on("click",".icon-trashcan",function (e) {
        var $tr=$(this).parents("tr");
       _as.jConfirm("<p>确定要用户【" +$tr.find("td:eq(1)").text()+"】？</p>",function () {
           ad.del("NU_U_DELETE",{id:$tr.attr("data-id")},function () {
               showUsers(curIndex, 1);
           });
       },null,tmplurl)

    })

    $("#dialog_addUser .btn-minor").click(function () {
        $("#dialog_addUser").hide();
    })

    $("#tbd_users").on("click",".icon-pencil",function (e) {
        var user=curentUsers[$(this).parents("tr").attr("data-id")];

        $("#account").val(user.account);
        $("#password").val(user.password);
        $("#realName").val(user.name);
        $("#phone").val(user.phone);
        $("#email").val(user.mail);
        $("#dialog_addUser textarea").val(user.note);
        $("#dialog_addUser input:checkbox").each(function () {this.checked=false;})
        var temp=user.auth.split(",");
        for(var i=0;i<temp.length;i++){
            $("[href='#']")
            $("#authSelect [data-value='" +temp[i]+"']")[0].checked=1;
        }
        $("#dialog_addUser").attr("data-id",user.id);
        $("#title").text("编辑用户");
        $("#dialog_addUser").show()
    })

    $("#dialog_addUser .icon-close").click(function () {
        $("#dialog_addUser").hide();
    })

    $("#add").click(function (e) {
        $("#dialog_addUser").attr("data-id","");
        $("#title").text("添加用户");
        $("#dialog_addUser input,textarea").val("");
        $("#dialog_addUser input:checkbox").each(function () {this.checked=false;})
        $("#dialog_addUser").show()
    })

    $("#save").click(function () {
        var id= $("#dialog_addUser").attr("data-id");
        var edite=id!=null&&id.toString().length>0;

        var user={
            account:$("#account").val(),
            password:$("#password").val(),
            name:$("#realName").val(),
            phone:$("#phone").val(),
            mail:$("#email").val(),
            auth:[],
            deleteable:edite? curentUsers[id].deleteable : 1 ,
            status:edite? curentUsers[id].status : 0 ,
            note:$("#dialog_addUser textarea").val()
        }

        $("#authSelect input:checkbox").each(function () {
            if(this.checked)user.auth.push($(this).attr("data-value"));
        })
        user.auth=user.auth.join(",");
        if(edite)user.id=id;


        if(user.account==null||user.account.length==0){
            _as.jAlert("<p>请填写账号名称</p>",null,null,tmplurl)
            return;
        }

        if(user.password==null||user.password.length==0){
            _as.jAlert("<p>请填写账号密码</p>",null,null,tmplurl)
            return;
        }

        if(user.name==null||user.name.length==0){
            _as.jAlert("<p>请填写真实姓名</p>",null,null,tmplurl)
            return;
        }

        if(user.auth==null||user.auth.length==0){
            _as.jAlert("<p>请填选择用户权限</p>",null,null,tmplurl)
            return;
        }

        var reg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
        if(user.mail.length>0&&!reg.test(user.mail)){
            _as.jAlert("<p>请正确填写邮箱地址</p>",null,null,tmplurl)
            return;
        }

        reg = /^((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/;
        if(user.phone.length>0&&!reg.test(user.phone)){
            _as.jAlert("<p>请正确填写电话号码</p>",null,null,tmplurl)
            return;
        }

        var api=edite? "NU_U_MODIFY":"NU_U_ADD";
        ad.insert(api,user,function (data) {
            if(data==1){
                showUsers(curIndex,1);
                $("#dialog_addUser").hide()
            }
            else {
                _as.jAlert("<p>保存失败:"+data +"</p>",null,null,tmplurl)
            }
        })
    })

    $("#btn_logout").click(function () {
       // window.location.href = appApi("LOGOUT");
        window.history.back();
    })

    ad.selectList("NUD_USER", {}, function (data) {
        $("#topUserName").html( data);
    })

    function  showUsers(index,r){
        var params = {
            "page": index,
            "pageSize": appApi("PAGE_SIZE"),
            "keyword": $("#txt_keyword").val()
        };
        ad.selectList("NU_U_LIST",params,function (data) {
            curIndex=index;
            curentUsers={};
            $("#tbd_users").empty();
            $.each(data.content,function (index,item) {
                curentUsers[item.id]=item;
                item=$.extend({},item);
                item.index=index+1;
                item.authStr=[];
                $.each(item.auth.split(','),function () {
                    item.authStr.push(authObj[this]);
                })
                item.authStr=item.authStr.join(",")

                var $tr=$.tmpl(tmplObj.tmpl,item);
                $tr.find("input:checkbox")[0].checked= Number(item.status)
                if(!item.deleteable) $tr.find(".icon-trashcan").parent().hide();
                $("#tbd_users").append($tr);
            })

            if(r)renderPager(data.totalRecord,index-1,$(".panel-foot .page"),showUsers)
        })

    }

    function renderPager(totalRecord, nowPage,$dom,fun) {
        $dom.pagination(totalRecord, appApi("PAGE_SIZE"), {
            callback: function(page_id, jq) {
                fun( page_id + 1,0);
            }
        }, nowPage);
    }

    function getTmpl() {
        tmplObj.tmpl =
            '<tr data-id="{{= id}}">\n' +
            '  <td>{{= index}}</td>\n' +
            '  <td><a href="#">{{= account}}</a></td>\n' +
            '  <td>{{= name}}</td>\n' +
            '  <td>{{= phone}}</td>\n' +
            '  <td>{{= mail}}</td>\n' +
            '  <td>{{= authStr}}</td>\n' +
            '  <td><label class="toggle"><input checked="" class="user_state" type="checkbox"> <span class="handle"></span></label></td>\n' +
            '  <td>{{= note}}</td>\n' +
            '  <td class="icons"><a href="#" title="修改"><i class="icon-pencil"></i></a><a href="#" title="删除"><i class="icon-trashcan"></i></a></td>\n' +
            '</tr>';
    }
})